<template>
  <div class="app-container">
    <!-- 查询框 -->
    <div class="filter-container">
      <el-input
        v-model="pageParam.superSearchKeyWord"
        style="width: 200px;"
        placeholder="关键字查询..."
        class="filter-item"
        @keyup.enter.native="handleFilter"
        @input="handleFilter"
      />
    </div>

    <!-- 表格 -->
    <el-table :data="list" fit highlight-current-row style="width: 100%;" @sort-change="sortChange">
      <el-table-column class="filter-item" type="expand">
        <template slot-scope="{row}">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item label="身份证号码">
              <span>{{ row.identityNo }}</span>
            </el-form-item>
            <el-form-item label="违约次数">
              <span>{{ row.violations }}</span>
            </el-form-item>
            <el-form-item label="医保号">
              <span>{{ row.medicalId }}</span>
            </el-form-item>
            <el-form-item label="联系人姓名" label-width="120px">
              <span>{{ row.relaName }}</span>
            </el-form-item>
            <el-form-item label="联系人电话">
              <span>{{ row.relaPhone }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column prop="realName" label="姓名" />
      <el-table-column :formatter="genderHandler" prop="gender" label="性别" />
      <el-table-column sortable="custom" prop="age" label="年龄" />
      <el-table-column prop="tellphone" label="电话" />
      <el-table-column prop="preMoney" label="预缴金额" />

      <el-table-column label="操作" fit>
        <template slot-scope="{row,$index}">
          <el-button size="small" type="primary" @click="handleUpdate(row)">编辑</el-button>
          <el-button size="small" v-if="row.status=='0'" type="danger" @click="handleDelete(row,1)">停用</el-button>
          <el-button size="small" v-if="row.status=='1'" type="success" @click="handleDelete(row,0)">激活</el-button>
        </template>
      </el-table-column>

    </el-table>

    <!-- 分页组件 -->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.pageNum"
      :limit.sync="listQuery.pageSize"
      @pagination="getListByPage"
    />
    <!-- 弹窗组件 -->
    <el-dialog title="更新信息" :visible.sync="dialogFormVisible">
      <el-form
        ref="dataForm"
        :model="temp"
        label-position="right"
        label-width="80px"
        style="width: 400px; margin-left:50px;"
      >

        <el-form-item label="电话">
          <el-input v-model="temp.tellphone" />
        </el-form-item>
        <el-form-item label="医保号">
          <el-input v-model="temp.medicalId" />
        </el-form-item>
        <el-form-item label="预缴金额">
          <el-input v-model="temp.preMoney" />
        </el-form-item>
        <el-form-item label="联系人姓名" label-width="85px">
          <el-input v-model="temp.relaName" />
        </el-form-item>
        <el-form-item label="联系人电话" label-width="85px">
          <el-input v-model="temp.relaPhone" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="updateData()">
          确认
        </el-button>
      </div>
    </el-dialog>

    <el-dialog :visible.sync="dialogPvVisible" title="Reading statistics">
      <el-table :data="pvData" border fit highlight-current-row style="width: 100%">
        <el-table-column prop="key" label="Channel" />
        <el-table-column prop="pv" label="Pv" />
      </el-table>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialogPvVisible = false">确认</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
// 导入user的api
import {
  searchSomePatientPage,
  updatePatient
} from '@/api/patient/patient'
// 导入组件  记得声明组件
import Pagination from '@/components/Pagination'

export default {
  components: { // 在这里声明组件
    Pagination
  },
  data() {
    return {
      depList: [],
      list: [], // 列表数据
      total: 0,
      listQuery: { // 分页 排序  查询参数
        pageNum: 1,
        pageSize: 5
      },
      fieldQuery: { // 按字段查询
        realName: ''
      },
      dialogFormVisible: false,
      dialogStatus: '',
      dialogPvVisible: false,
      pvData: [],
      temp: {
        tellphone: '',
        relaName: '',
        relaPhone: '',
        medicalId: '',
        preMoney: ''
      },
      pageParam: {
        pageNum: 1,
        pageSize: 5,
        model: {},
        orderParams: [],
        superSearchKeyWord: ''
      },
      roleList: []
    }
  },
  created() {
    this.getListByPage()
  },
  methods: {
    handleDelete(row, status) {
      updatePatient({ id: row.id, status: status }).then(() => {
        this.dialogFormVisible = false
        this.$notify({
          title: '成功',
          message: '编辑成功',
          type: 'success',
          duration: 2000
        })
        this.getListByPage()
      })
    },
    genderHandler(row) {
      if (row.gender == '0') {
        return '女'
      }
      return '男'
    },
    getListByPage() {
      this.pageParam.pageNum = this.listQuery.pageNum
      this.pageParam.pageSize = this.listQuery.pageSize

      console.log(this.pageParam.orderParams)

      searchSomePatientPage(this.pageParam).then(res => {
        // console.log(res)
        this.list = res.content.list
        this.total = res.content.total
      }).catch(err => {
        console.log(err)
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getListByPage()
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row) // copy obj
      this.dialogFormVisible = true
    },
    updateData() {
      updatePatient(this.temp).then(() => {
        this.dialogFormVisible = false
        this.$notify({
          title: '成功',
          message: '编辑成功',
          type: 'success',
          duration: 2000
        })
        this.getListByPage()
      })
    },
    sortChange(val) {
      const prop = val.prop
      const order = val.order === 'ascending' ? 'asc' : 'desc'
      let orderParam = 'age' + ' ' + order
      this.pageParam.orderParams = []
      this.pageParam.orderParams.push(orderParam)
      this.getListByPage()
    },
  }
}
</script>

<style>
.demo-table-expand {
  font-size: 0;
}

.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}

.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
